﻿function init() {
    gettree();
    if ($.getPara('op') == 'edit') {
        $('#opbtn').show();
    }
}

function exit() {
    parent.$.closePage();
}

//#region 加载菜单数据
function gettree() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryAllTree",
        model: function () { return 0; },
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PObjectID == '0') {
                        var obj = {};
                        obj.id = data[i].ObjectID;
                        obj.text = data[i].ObjectName + ' <select id="level_' + data[i].ObjectID + '"><option value="1">' + lang.Online_Browse_file + '</option><option value="2">' + lang.Download_PDF_format_file + '</option><option value="3">' + lang.Download_the_original_file + '</option></select>';

                        getsubmenu(data, data[i].ObjectID, obj);
                        _data.push(obj);
                    }
                }
                _menudata = data;
                $('#rolerights').tree('loadData', _data);
                $('#rolerights').tree({
                    cascadeCheck: false
                    /*,
                    onCheck: function (node, checked) {
                        if (checked) {
                            var parentNode = $("#rolerights").tree('getParent', node.target);
                            if (parentNode != null) {
                                $("#rolerights").tree('check', parentNode.target);
                            }
                        } else {
                            var childNode = $("#rolerights").tree('getChildren', node.target);
                            if (childNode.length > 0) {
                                for (var i = 0; i < childNode.length; i++) {
                                    $("#rolerights").tree('uncheck', childNode[i].target);
                                }
                            }
                        }
                    }*/
                });
                if ($.getPara('roleid') != '') {
                    bindrolerights();
                }
            }

        }
    });

    function getsubmenu(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PObjectID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ObjectID;
                    obj.text = _d[i].ObjectName + ' <select id="level_' + _d[i].ObjectID + '"><option value="1">' + lang.Online_Browse_file + '</option><option value="2">' + lang.Download_PDF_format_file + '</option><option value="3">' + lang.Download_the_original_file + '</option></select>';
                    
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubmenu(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

//#region 绑定角色权限
function bindrolerights() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryRoleTreeByID",
        model: function () {
            return $.getPara('roleid');
        },
        success: function (data) {
            if (data && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var node = $('#rolerights').tree('find', data[i].MENU_ID);
                    $('#rolerights').tree('check', node.target);
                    $('#level_' + data[i].MENU_ID).val(data[i].RightsLevel);
                }
            }
        }
    });
}
//#endregion

//#region 保存
function save() {
    var menulist = $('#rolerights').tree('getChecked');
    if (menulist.length < 1) {
        alert(lang.No_permissions_configuration_node);
        return false;
    }
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "EditRoleTree",
        model: function () {
            var obj = {};
            obj.RoleID = $.getPara('roleid');
            var rightsarr = [];
            //var menulist = $('#rolerights').tree('getChecked');
            var treerolearr = [];
            if (menulist && menulist.length > 0) {
                for (var i = 0; i < menulist.length; i++) {
                    var _treerole = {};
                    _treerole.ObjID = menulist[i].id;
                    _treerole.RightsLevel = $('#level_' + menulist[i].id).val();
                    treerolearr.push(_treerole);
                }
            }
            obj.FileRightsList = treerolearr;
            return $.toJSON(obj)
        },
        success: function (data) {
            if (data) {
                $.alert(lang.Object_tree_assignment_complete, "", function () { parent.$.closePage(); });
            }
        }
    });
}
//#endregion